// Ionic_SDK
// 
// 保佑没Bug 
//
// Created by AS on 2019/6/3. 
//
// Copyright © 2018 AS. All rights reserved.
// 

#import "InputView.h"

@interface InputView()<UITextFieldDelegate>
/**  */
@property (nonatomic,strong) UITextField *textField;
/**  */
@property (nonatomic,strong) UIButton *replyBtn;
@end

@implementation InputView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubView];
    }
    return self;
}


-(void)initSubView{
    self.backgroundColor = [UIColor whiteColor];
    
    UIView *view = [UIView new];
    view.backgroundColor = HexRGB(0xf3f5f5);
    [self addSubview:view];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_offset(1);
    }];
    
    self.textField = [UITextField new];
    self.textField.delegate = self;
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    self.textField.placeholder = @"回复";
    self.textField.returnKeyType = UIReturnKeySend;
    [self.textField addTarget:self action:@selector(textEditAction:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:self.textField];
    
    self.replyBtn = [UIButton new];
    self.replyBtn.enabled = NO;
    [self.replyBtn setBackgroundImage:[UIImage createImageWithColor:HexRGB(0xaaaaaa)] forState:UIControlStateDisabled];
    [self.replyBtn setBackgroundImage:[UIImage createImageWithColor:mMainColor] forState:UIControlStateNormal];
    [self.replyBtn setTitle:@"回复" forState:UIControlStateNormal];
    [self.replyBtn addTarget:self action:@selector(replyAction) forControlEvents:UIControlEventTouchUpInside];
    [self.replyBtn setLayerWithRadius:5 color:ClearColor width:0];
    [self addSubview:self.replyBtn];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self).offset(5);
        make.height.mas_offset(39);
        make.right.equalTo(self.replyBtn.mas_left).offset(-5);
    }];
    
    [self.replyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(60);
        make.height.mas_offset(39);
        make.centerY.equalTo(self.textField);
        make.right.equalTo(self).offset(-5);
    }];
}

-(void)textEditAction:(UITextField *)textField{
    self.replyBtn.enabled = textField.text.length > 0;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    textField.placeholder = @"回复";
    if ([self.delegate respondsToSelector:@selector(inputViewDidEndText)]) {
        [self.delegate inputViewDidEndText];
    }
}

-(void)setPlaceholder:(NSString *)placeholder{
    [self.textField becomeFirstResponder];
    self.textField.placeholder = placeholder;
}


-(void)replyAction{
    
    if ([self.delegate respondsToSelector:@selector(inputViewSendText:)]) {
        [self.delegate inputViewSendText:self.textField.text];
        [self.textField endEditing:YES];
        self.textField.text = @"";
    }
}


@end
